﻿package lz.controls.managers
{
    import com.common.utils.Reflection;
    import com.global.GlobalDef;
    
    import flash.display.DisplayObject;
    import flash.utils.Dictionary;
	/**
	 *Tips 管理 
	 * @author lake
	 *  item.addEventListener(MouseEvent.ROLL_OVER,onRollOver);
		item.addEventListener(MouseEvent.ROLL_OUT,onRollOut);
		function onRollOver(e :MouseEvent):void
		{
			TipsManager.show(TipsConst.COMMONTIPS,"测试",15,15);
		}
		function onRollOut(e :MouseEvent):void
		{
			TipsManager.hide();
		}
	 */
    public class TipsManager
    {
        private var curTip				:DisplayObject;
        private var tipClass			:Dictionary;
        private static var _instance	:TipsManager;

        public function TipsManager()
        {
        }
		/**
		 *显示tips 
		 * @param tipKey tip类名
		 * @param obj tip的数据
		 * @param offsetX x偏移量
		 * @param offsetY y偏移量
		 * @param isMove 是否随着鼠标移动 默认true
		 * @param isResize 是否适应位置，默认false，一直显示右下方
		 * 
		 */		
        private function show(tipKey:String, 
							  obj:Object,
							  offsetX:int = 0,
							  offsetY:int = 0,
							  isMove :Boolean = true,
							  isResize:Boolean = false) : void
        {
            if (GlobalDef.GameStage == null)
            {
                return;
            }
            createTip(tipKey);
            if (!(curTip is DisplayObject))
            {
                return;
            }
            curTip["data"] = obj;
            GlobalDef.GameStage.addChild(curTip);
            curTip.x = GlobalDef.GameStage.mouseX + offsetX;
            curTip.y = GlobalDef.GameStage.mouseY + offsetY;
            MoveManager.startMove(curTip,isMove,isResize);
        }

        private function destroy() : void
        {
            if (curTip)
            {
                MoveManager.stopMove(curTip);
                curTip["destroy"]();
                if (curTip.parent)
                {
                    curTip.parent.removeChild(curTip);
                }
                curTip = null;
            }
        }

        private function createTip(tipKey:String) : void
        {
            var c:Class;
            c = tipClass[tipKey];
            if (c == null)
            {
                return;
            }
            try
            {
                curTip = new c;
            }
            catch (e:Error)
            {
               
            }
        }

        public static function hide() : void
        {
            instance.destroy();
        }
		/**
		 * show
		 * @param tipKey -- tip class name String
		 * @param obj
		 * @param offsetX
		 * @param offsetY
		 * @param isMove 是否随着鼠标移动
		 * @param isResize 是否适应位置，默认false，一直显示右下方
		 */
        public static function show(tipKey:String, 
									obj:Object, 
									offsetX:int = 15, 
									offsetY:int = 15,
									isMove:Boolean = true,
									isResize:Boolean = false) : void
        {
            if (GlobalDef.GameStage == null)
            {
                return;
            }
            if (instance.curTip && instance.curTip["data"] != obj)
            {
                instance.destroy();
            }
            instance.show(tipKey, obj, offsetX, offsetY,isMove,isResize);
        }

        private static function get instance() : TipsManager
        {
            if (_instance == null)
            {
                _instance = new TipsManager();
                _instance.tipClass = new Dictionary();
            }
            return _instance;
        }
		/**
		 * register
		 * @param tipClass
		 * 
		 */		
		public static function register(tipClass:Class) : void
		{
			var className :String = Reflection.getClassName(tipClass);
			if (className != null && className != "")
			{
				instance.tipClass[className] = tipClass;
			}
		}
		/**
		 * unRegister
		 * @param tipKey
		 * 
		 */
        public static function unRegister(tipKey:String) : void
        {
            delete instance.tipClass[tipKey];
        }
		/**
		 * hasRegister
		 * @param tipKey
		 * @return 
		 * 
		 */
        public static function hasRegister(tipKey:String) : Boolean
        {
            return instance.tipClass[tipKey];
        }
    }
}
